﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.LibarySys
{
    public partial class ViewScrapApply : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                if (Session["USER_NAME"] != null)
                {
                    Approver.Text = Session["USER_NAME"].ToString();
                }
                if (Request["sign"] != null)
                {
                    if (Request["sign"] == "1")
                    {
                        btnPass.Visible = true;
                        btnReject.Visible = true;
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = true;
                    }
                    else if (Request["sign"] == "2")
                    {
                        btnPass.Visible = true;
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = true;
                    }
                    else if (Request["sign"] == "3")
                    {
                        btnPass.Visible = false;
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = true;
                        MyNotation.ReadOnly = true;
                    }
                    else if (Request["sign"] == "4")
                    {
                        btnPass.Visible = false;
                        btnReturnA.Visible = true;
                        btnReturnE.Visible = false;
                        MyNotation.ReadOnly = true;
                    }
                    //else if (Request["sign"] == "5")
                    //{
                    //    btnPass.Visible = false;
                    //    btnReturnA.Visible = false;
                    //    btnReturnE.Visible = false;
                    //    MyNotation.ReadOnly = true;
                    //}
                    else
                    {
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = false;
                        MyNotation.ReadOnly = true;
                    }
                }
                else
                {
                    MyNotation.ReadOnly = true;
                }
                if (Request["id"] != null)
                {
                    hidType.Value = "edit";
                    string id = Request["id"];
                    BindDate(id);
                }
            }
        }
        #region  绑定数据
        private void BindDate(string id)
        {
            int scrap_id;
            int.TryParse(id, out scrap_id);
            Model.LibrarySys.T_PUB_SCRAP scrap = new Model.LibrarySys.T_PUB_SCRAP();
            BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
            scrap = bll.GetModel(scrap_id);
            Applicants.Text = scrap.APPLICANT_NAME;
            int branch_id = int.Parse(scrap.BRANCH_ID.ToString());
            BindBranchName(branch_id);
            int library_id = int.Parse(scrap.LIBRARY_ID.ToString());
            BindLibraryName(library_id);
            //Applications.Text = libraryout.NUM.ToString();
            //int material_id = int.Parse(libraryout.MATERIAL_ID.ToString());
            //BindMaterial(id);
            Material_Name.Text = scrap.MATERIAL_NAME;
            Material_Num.Text = scrap.NUM.ToString();
            hidPid.Value = scrap.ID.ToString();
            Remark.Text = scrap.REMARK;
            Titel.Text = scrap.TITEL;
            if (scrap.APPROVER_NAME != null && scrap.APPROVER_NAME != "")
            {
                Approver.Text = scrap.APPROVER_NAME;
            }
            MyNotation.Text = scrap.NOTATION;
        }
        #endregion

        #region 绑定仓库
        private void BindLibraryName(int library_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            Model.SysManage.T_PUB_LIBRARY library = bll.GetModel(library_id);
            if (library != null)
            {
                this.LibrayName.Text = library.LIBRARY_NAME;
                //this.LibraryID.Value = library.LIBRARY_ID.ToString();
            }
        }
        #endregion

        #region 绑定分店列表
        private void BindBranchName(int branch_id)
        {
            Model.SysManage.T_PUB_BRANCH branch = new Model.SysManage.T_PUB_BRANCH();
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            branch = LibraryBll.GetModel(branch_id);
            BranchName.Text = branch.BRANCH_NAME;
        }
        #endregion

        protected void btnPass_Click(object sender, EventArgs e)
        {
            bool flag = true;
            Model.LibrarySys.T_PUB_SCRAP model = new Model.LibrarySys.T_PUB_SCRAP();
            BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
            int scrap_id = int.Parse(hidPid.Value);
            model = bll.GetModel(scrap_id);
            model.NOTATION = MyNotation.Text;
            model.APPROVE_TIME = DateTime.Now;
            model.APPROVER = int.Parse(Session["USER_ID"].ToString());
            model.APPROVER_NAME = Session["USER_NAME"].ToString();
            model.STATE = "3";
            //if()标示是出库损失或是报废
            if (model.FLAG == "0")
            {
                int library_id = int.Parse(model.LIBRARY_ID.ToString());
                int material_id = int.Parse(model.MATERIAL_ID.ToString());
                Model.LibrarySys.T_PUB_LIBRARYMATERIAL lmmodel = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
                BLL.LibrarySys.T_PUB_LIBRARYMATERIAL lmbll = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
                lmmodel = lmbll.GetModel(library_id, material_id);
                if (lmmodel != null)
                {
                    if (lmmodel.NUM >= model.NUM)
                    {
                        lmmodel.NUM -= model.NUM;
                        bll.UpdateLibraryMaterial(model, lmmodel);
                        Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "报销申请" + model.TITEL + " 状态为已审批", DateTime.Now);
                        flag = true;
                        Response.Redirect("ExamineScrapList.aspx");
                    }
                    else
                    {
                        flag = false;
                    }

                }
                else
                {
                    flag = false;
                }
            }
            else
            {
                if (!bll.Update(model))
                {
                    Universal.JS.Alert("审批不成功！请您重新设置审批！");
                }
            }
            if (flag == false)
            {
                Universal.JS.Alert("仓库中该元素库存量少于报销数量！审批不成功！请您重新设置审批！");
            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            bool flag;
            Model.LibrarySys.T_PUB_SCRAP model = new Model.LibrarySys.T_PUB_SCRAP();
            BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
            int scrap_id = int.Parse(hidPid.Value);
            model = bll.GetModel(scrap_id);
            model.NOTATION = MyNotation.Text.Trim();
            model.APPROVE_TIME = DateTime.Now;
            model.APPROVER = int.Parse(Session["USER_ID"].ToString());
            model.APPROVER_NAME = Session["USER_NAME"].ToString();
            model.STATE = "2";
            //if()标示是出库损失或是报废
            model.FLAG = "0";
            flag = bll.Update(model);
            Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "报销申请" + model.TITEL + " 状态为审批不通过", DateTime.Now);
            if (flag == true)
            {
                Response.Redirect("ExamineScrapList.aspx");
            }
            else
            {
                Universal.JS.Alert("审批不成功！请您重新设置审批！");
            }
        }

        protected void btnReturnA_Click(object sender, EventArgs e)
        {
            Response.Redirect("ScrapList.aspx");
        }

        protected void btnReturnE_Click(object sender, EventArgs e)
        {
            Response.Redirect("ExamineScrapList.aspx");
        }
    }
}